<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	小程序直播
*/
class live extends \plugin{
	//更新log
	public function uplooklog($rooms=array(),$fromuser='',$isout=false,$noup=false,$islogin=false){
		if(!$rooms['roomid']){
			return array();
		}
		$log=pdo_get('core_account_wxapp_live_looklog',array('live_id'=>$rooms['live_id'],'roomid'=>$rooms['roomid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$log['id']){
			$log=array(
				'live_id'=>$rooms['live_id'],
				'roomid'=>$rooms['roomid'],
				'live_roomid'=>trim($rooms['id']),
			);
			$log['uniacid']=$_SESSION['uniacid'];
			$log['mid']=$_SESSION['uid'];
			$log['fromuser']=$fromuser?:0;
			$log['createtime']=TIMESTAMP;
			if($isout){
				$log['outtime']=TIMESTAMP;
				$log['isout']=1;
				$log['uptime']=0;
			}else{
				$log['uptime']=TIMESTAMP;
				$log['isout']=0;
			}
			pdo_insert('core_account_wxapp_live_looklog',$log);
			$log['id']=pdo_insertid();
		}elseif(!$noup){
			$data=array(
				'nowlooktime'=>0,
			);
			if($log['uptime']>10000 && !$islogin){
				$islooktime=TIMESTAMP-$log['uptime'];
				$data['islooktime +=']=$islooktime;	
			}
			if($isout){
				$data['outtime']=TIMESTAMP;	
				$data['isout']=1;
				$data['uptime']=0;
			}else{
				$data['uptime']=TIMESTAMP;
				$data['isout']=0;
			}
			if(!$log['fromuser'] && $fromuser){
				$data['fromuser']=$fromuser;
			}
			pdo_update('core_account_wxapp_live_looklog',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['id']));
			$log=array_merge($log,$data);
			$this->getjiang($log);
		}
		return $log;
	}
	public function addrooms($roomid=0,$live_id=0){
		$rooms=array(
			'name'=>'匿名创建',
			'roomid'=>$roomid,
		);
		$rooms['createtime']=TIMESTAMP;
		$rooms['live_id']=$live_id;
		$rooms['uniacid']=$_SESSION['uniacid'];
		if($roomid){
			pdo_insert('core_account_wxapp_live_rooms',$rooms);
			$rooms['id']=pdo_insertid();
		}
		return $rooms;
	}
	public function startlooktime($fromuser=''){
		$rooms=pdo_getall('core_account_wxapp_live_rooms',array('live_status'=>101,'uniacid'=>$_SESSION['uniacid']));
		foreach($rooms as $rv){
			$this->model('live')->uplooklog($rv,$fromuser,'','',true);
		}
		return true;
	}
	public function endlooktime(){
		$rooms=pdo_getall('core_account_wxapp_live_rooms',array('live_status'=>101,'uniacid'=>$_SESSION['uniacid']));
		foreach($rooms as $rv){
			$looklog=pdo_get('core_account_wxapp_live_looklog',array('live_id'=>$rv['live_id'],'roomid'=>$rv['roomid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if($looklog['uptime']>0){
				$islooktime=TIMESTAMP-$looklog['uptime'];
				pdo_update('core_account_wxapp_live_looklog',array('islooktime +='=>$islooktime,'uptime'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$looklog['id']));
				$looklog['islooktime'] +=$islooktime;
				$this->getjiang($looklog);
			}
		}
		pdo_update('core_account_wxapp_live_looklog',array('uptime'=>0),array('uniacid'=>$_SESSION['uniacid'],'uptime >'=>100,'mid'=>$_SESSION['uid']));
		return true;
	}
	public function checkendtime($live_id=0){
		global $_RQ;
		if(!$live_id){
			$live_id=intval($_RQ['live_id']);
			if($live_id){
				$this->session('live_id',$live_id);
			}
			if($_SESSION['wxapp_id']){
				$live_id=pdo_getcolumn('core_account_wxapp_live',array('wxapp_id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),'id');
				$this->session('live_id',$live_id);
			}
			$live_id=$this->session('live_id');
			if(!$live_id){
				if($sale_live['live_id']){
					$live_id=$sale_live['live_id'];
				}else{
					$live_id=pdo_getcolumn('core_account_wxapp_live',array('uniacid'=>$_SESSION['uniacid']),'id');
				}
				$this->session('live_id',$live_id);
			}
		}
		if($_RQ['fromroomid'] || $_RQ['room_id']){//来自直播小程序的直播间
			if($_RQ['room_id']){
				pdo_update('core_account_wxapp_live_rooms',array('fromlook +='=>1),array('roomid'=>$_RQ['room_id'],'uniacid'=>$_SESSION['uniacid']));
				$rooms=pdo_get('core_account_wxapp_live_rooms',array('roomid'=>$_RQ['room_id'],'uniacid'=>$_SESSION['uniacid']));
				$_RQ['fromroomid']=$rooms['id'];
			}
			$_RQ['fromroomid']=$_RQ['fromroomid']?:$_RQ['room_id'];
			if(!$rooms){
				$rooms=$this->model('live')->addrooms($_RQ['fromroomid'],$live_id);
			}
			if($_RQ['share_openid']){
				$fromuser=pdo_getcolumn('core_fans',array('type'=>1,'openid'=>$_RQ['share_openid'],'wxgid'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),'mid');
			}
			$this->model('live')->uplooklog($rooms,$fromuser,true);
			$_SESSION['room_id']=$_RQ['fromroomid'];
		}
		$upnum=pdo_count('core_account_wxapp_live_rooms',array('live_status'=>101,'live_id'=>$live_id,'uniacid'=>$_SESSION['uniacid']));
		$lognum=pdo_count('core_account_wxapp_live_looklog',array('uptime >'=>0,'live_id'=>$live_id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$upnum && $lognum>0){
			pdo_update('core_account_wxapp_live_looklog',array('uptime'=>0),array('uptime >'=>0,'live_id'=>$live_id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function getjiang($looklog=array()){
		$goods=$this->plugin_setting('sale_live');
		$looklog['looktimefen']=$looklog['islooktime']/60;
		$goods['huo_set']=$goods['lookjiang_set'];
		foreach($goods['huo_set'] as $hv){
			$getlog=pdo_get('core_account_wxapp_live_getlog',array('getfen'=>$hv['fen'],'roomid'=>$looklog['roomid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if($looklog['looktimefen']>=$hv['fen'] && !$getlog['id']){
				$isget=1;
				$data=array(
					'live_id'=>$looklog['live_id'],
					'live_roomid'=>$looklog['live_roomid'],
					'roomid'=>$looklog['roomid'],
					'getfen'=>$hv['fen'],
					'fee'=>$hv['num'],
				);
				$data['uniacid']=$_SESSION['uniacid'];
				$data['mid']=$_SESSION['uid'];
				$data['createtime']=TIMESTAMP;
				pdo_insert('core_account_wxapp_live_getlog',$data);
				cfc('core')->member_credit($_SESSION['uid'],'credit1',$hv['num'],1,'jy_weishop','看直播领奖励');
			}
		}
		return true;
	}
}